import Person.LocalUserInfo;
import Person.ReadFile;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/LocalLoginServlet")
public class LocalLoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        LocalUserInfo user = new ReadFile().find(username);
        if(user!=null){
            if(user.getPassword().equals(request.getParameter("passwd"))){
                Map<String, String> info = new HashMap<String, String>();
                info.put("username", user.getUsername());
                info.put("name", user.getName());
                info.put("faculty_title", user.getFaculty_title());
                info.put("group", user.getGroup());
                HttpSession session = request.getSession(true);
                session.setAttribute("user", info);
                response.sendRedirect("/info.jsp");
            }else {
                response.getWriter().println("密码不正确！！");
            }
        }else {
            response.getWriter().println("用户不存在！！");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
